import _ from 'lodash'
import { article } from './lablog.js'
import {Maybe, map} from './util.js'
const safeProp = _.curry(function(x, obj) {
    // console.log("safe prop", obj)
    return new Maybe(obj[x])
})
const safeHead = safeProp(0)
const firstAddressStreet = _.flowRight(
    map(map(safeProp('street'))),
    map(safeHead),
    safeProp('addresses')
)


const addressInfo = {
    addresses: [
        {street: "hello", postcode: 'ww'}
    ]
}

console.log(firstAddressStreet(addressInfo))

/**
 * firstAddress(addressInfo) =>
 * 1. safeProp(address)(addreessInfo) => Maybe([{street}])
 * 2. map(safeHead)(Maybe([{street}])) =>
 *  2.1 Maybe([{street}]).map(safeHead)
 *  2.2 Maybe.of(safeHead([{street}]))
 *  2.3 Maybe(Maybe(street))
 * 3. map(map(safeProp('street')))(Maybe(Maybe(street)))
 *  3.1 Maybe(Maybe(street)).map(map(safeProp('street')))
 *  3.2 Maybe(map(safeProp('street'))(Maybe(street))
 *  3.3 Maybe(Maybe(safeProp('street')(street)))
 *  3.4 Maybe(Maybe(Maybe(value)))
 */

article(`
1. 通常来说，每个 functor 执行 map(f) 之后,总会包裹到容器里。即 Container.of(f(v))
2. 这样一来，相当于有几个 map 就会有 几层 Container
3. 此外，如果 f 本身返回的是 Container 的话，map(f) 就会嵌套一层, 即 map(f) 就会有两层。
`)